﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Sgr.DataCategories.Stores
{
    /// <summary>
    /// 本地数据字典存储接口
    /// </summary>
    public interface IDataDictionaryStore : IDisposable
    {
        /// <summary>
        /// 获取缓存的数据字典，如果不存在则通过提供的工厂方法添加
        /// </summary>
        /// <param name="category">数据字典类别</param>
        /// <param name="valueFactory">用于获取数据字典项的工厂方法</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>数据字典项的只读集合</returns>
        Task<IReadOnlyDictionary<string, NameValue>> GetOrAddAsync(
            string category,
            Func<string, CancellationToken, Task<IReadOnlyDictionary<string, NameValue>>> valueFactory,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 刷新指定类别的数据字典
        /// </summary>
        /// <param name="category">数据字典类别</param>
        /// <param name="items">新的数据字典项</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>表示异步操作的任务</returns>
        Task RefreshAsync(
            string category,
            IReadOnlyDictionary<string, NameValue> items,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 从缓存中移除指定类别的数据字典
        /// </summary>
        /// <param name="category">要移除的数据字典类别</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>表示异步操作的任务</returns>
        Task RemoveAsync(
            string category,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 清空所有缓存的数据字典
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>表示异步操作的任务</returns>
        Task ClearAsync(
            CancellationToken cancellationToken = default);
    }
}